package 设计模式;

public class LazySingleton {
    
    private String no;

    // 某个类只能有一个实例
    private static LazySingleton instance = null;

    // 必须由自己创建实例
    private LazySingleton(){}

    // 必须自己向系统提供实例
    public static LazySingleton getInstance(){
        if(instance == null){
            System.out.println("first use...create instance...");
            instance = new LazySingleton();
            instance.setNo("xxxxxxxxxxxxxxxxxx");
        } else {
            System.out.println("not first...get old...");
        }
        return instance;
    }
    public String getNo(){
        return no;
    }
    public void setNo(String no){
        this.no = no;
    }
    public static void main(String[] args) {
        LazySingleton s1 = LazySingleton.getInstance();
        LazySingleton s2 = LazySingleton.getInstance();
        System.out.println(s1.getNo());
        System.out.println(s1 == s2);
    }
}
